//The Plane Object class, inherits from Object

#include "Plane.h"

//The generic plane contructor
Plane::Plane() {
	name = "Plane";
	positiveDiscriminant = true;
	t1 = t2 = -1;
	issphere = false;
	boundingVolume = false;
}

Plane::Plane(Vector3 point, Vector3 normal) {
	Plane();
	n = normal;
	n.normalize();
	distanceFromOrigin = point.length();
}

//Returns the normal of the plane
Vector3 Plane::normal(Vector3 p) {
	return n;
}

//Determines the distance at which a ray intersects the Plane
Object* Plane::tIntersect(const Ray& ray, float& distance) {
	Vector3 rayDirection = ray.direction;
	Vector3 rayOrigin = ray.origin;

	float d = Vector3::dotProduct(n, rayDirection);
	t1 = t2 = ((Vector3::dotProduct(n, rayOrigin-Vector3(0, 0, 0)) + distanceFromOrigin) * (-1)) / d;

	distance = t1;
	return this;
}